package com.tanhua.server.service;

import com.tanhua.autoconfig.templates.HuanXinTemplate;

import com.tanhua.domain.db.UserInfo;

import com.tanhua.domain.mongo.Comment;

import com.tanhua.domain.vo.CommentVo;

import com.tanhua.domain.vo.MessageLikeVo;

import com.tanhua.domain.vo.PageResult;

import com.tanhua.dubbo.api.db.UserInfoApi;

import com.tanhua.dubbo.api.mongo.CommentApi;

import com.tanhua.dubbo.api.mongo.FriendApi;

import com.tanhua.server.interceptors.UserHolder;

import org.apache.dubbo.config.annotation.DubboReference;

import org.bson.types.ObjectId;

import org.checkerframework.checker.units.qual.C;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;

import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

@Service

public class CommentService {

    @DubboReference
    private CommentApi commentApi;
    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    public ResponseEntity queryComments(Integer page, Integer pagesize, String movementId,Integer commentType) {

//        1、mongo  2、mysql

        PageResult pageResult  = commentApi.queryComments(page,pagesize,movementId,commentType);
        List<Comment> commentList = (List<Comment>) pageResult.getItems();

        List<CommentVo> list = new ArrayList<>();
        for (Comment comment : commentList) {
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());

            CommentVo commentVo = new CommentVo();
            commentVo.setId(comment.getId().toString());
            commentVo.setAvatar(userInfo.getAvatar());
            commentVo.setNickname(userInfo.getNickname());
            commentVo.setContent(comment.getContent());
            commentVo.setCreateDate(new SimpleDateFormat("HH:mm").format(comment.getCreated()));
            commentVo.setLikeCount(comment.getLikeCount());

//            commentVo.setHasLiked();  TODO

            list.add(commentVo);
        }

        pageResult.setItems(list);

        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity saveComments(Map<String,String> param) {
        //        保存comment  圈子评论数+1
        Comment comment = new Comment();

        if (param.get("video").equals("1")) {
            comment.setPublishId(new ObjectId(param.get("videoId")));
        } else {
            comment.setPublishId(new ObjectId(param.get("movementId"))); // 圈子id
        }
        comment.setCommentType(Integer.parseInt(param.get("commentType"))); //评论类型，1-动态点赞，2-动态评论，3-动态喜欢 4-视频点赞  5-视频评论  6-评论点赞
        comment.setContent(param.get("comment")); //评论的内容
        comment.setUserId(UserHolder.getUserId());  //

//        comment.setLikeCount(); //这个评论收到的喜欢数

        Integer count =  commentApi.save(comment);
        return ResponseEntity.ok(null);

    }

    public ResponseEntity queryLikesOrCommentsOrLoves(Integer page, Integer pagesize, int commentType) {

//        1、查询mongo  Comment

        PageResult pageResult =   commentApi.queryLikesOrCommentsOrLoves(UserHolder.getUserId(),page,pagesize,commentType);
        List<Comment> commentList = (List<Comment>) pageResult.getItems();

        List<MessageLikeVo> list = new ArrayList<>();
        for (Comment comment : commentList) {

//            2、查询mysql UserInfo

//            Comment+  UserInfo = MessageLikeVo

            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            MessageLikeVo messageLikeVo = new MessageLikeVo();
            messageLikeVo.setId(comment.getId().toString());
            messageLikeVo.setAvatar(userInfo.getAvatar());
            messageLikeVo.setNickname(userInfo.getNickname());
            messageLikeVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(comment.getCreated()));
            list.add(messageLikeVo);
        }
        pageResult.setItems(list);

        return ResponseEntity.ok(pageResult);



    }

    @DubboReference
    private FriendApi friendApi;

    public ResponseEntity contacts(Long friendId) {

//        1、两个人成为好友

        friendApi.save(UserHolder.getUserId(),friendId);

//        2、在环信上成为好友

        huanXinTemplate.contactUsers(UserHolder.getUserId(),friendId);
        return ResponseEntity.ok(null);
    }

}
